package com.base.system.managedBean.baseBean;

import java.io.Serializable;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Validate;
import org.operamasks.faces.component.form.impl.UITextField;

import com.base.commons.SpringBeanHolder;
import com.base.security.entity.Resource;
import com.base.security.service.SecurityEntityManager;

public abstract class BaseResourceBean implements Serializable {
	protected SecurityEntityManager securityEntityManager = SpringBeanHolder
			.getBean("securityEntityManager");

	@Accessible
	@Bind(id = "id", attribute = "binding")
	protected UITextField idField;

	@Bind(id = "id", attribute = "value")
	protected Integer id;
	@Bind(id = "value", attribute = "value")
	protected String value;
	@Bind(id = "displayName", attribute = "value")
	protected String displayName;
	@Bind(id = "type", attribute = "value")
	protected String type;
	@Bind(id = "description", attribute = "value")
	protected String description;
	@Bind(id = "code", attribute = "value")
	protected String code;
	protected Resource resource;

	@Bind(id = "module", attribute = "value")
	protected String module;

	protected void setResourceProperty() {
		resource.setDescription(description);
		resource.setDisplayName(displayName);
		resource.setType(type);
		resource.setValue(value);
		resource.setCode(code);
		resource.setModule(module);
	}

	protected void getResourceProperty() {
		if (resource == null) {
			return;
		}
		id = resource.getId();
		description = resource.getDescription();
		displayName = resource.getDisplayName();
		type = resource.getType();
		value = resource.getValue();
		code = resource.getCode();
		module = resource.getModule();
	}

	@Validate(id = { "value" }, message = "资源值已经存在，请您选择其他值！")
	public boolean validateValue(String value) {
		Resource r = securityEntityManager.getResourceByValue(value);
		if (r == null)
			return true;
		else {
			// System.out.println(r.getId());
			// System.out.println("id:"+idField.getSubmittedValue());
			if (idField != null && idField.getSubmittedValue() != null) {
				if (idField.getSubmittedValue().toString().equals(
						r.getId().toString())) {
					// System.out.println("equals");
					return true;
				}
			}
			return false;
		}
	}
}
